home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4279 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.4 KB  |  39 lines

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help with Bug Pleeeeease!
  5. Date: 29 Jan 1996 14:59:00 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4einbk$ia9@colossus.holonet.net>
  8. References: <tday-2801961838030001@tday.slip.netcom.com>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Tony Day (tday@netcom.com) wrote:
  13.  
  14. : I hope some C++ wizard out there can help me debug this apparantly simple
  15. : program.  I am teaching myself C++ with Prata╣s │C++ primer +▓ and am
  16. : completely stuck with one of the problems at the end of CH11.
  17.  
  18. Well, I found one bug that would explain the crash:
  19.  
  20. : String String::operator+(const String & st) const
  21. : {
  22. ...
  23. :    temp1.str = new char[temp1.len+1];
  24. :    strcpy(temp1.str,str);
  25. :    for (int i=len; i< temp1.len; i++)
  26. :       temp1.str[i]=st.str[i-len];
  27. :    temp1.str[temp1.len+1]='\0';
  28.                ^^^^^^^^^^^
  29. Don't forget, the array starts at 0 so the last array element is
  30. temp1.str[temp1.len].  You are storing into the byte AFTER the end
  31. of the string.  Crash!
  32.  
  33. Actually, you could avoid a lot of trouble if you just replace your
  34. for loop with another strcpy:
  35.      strcpy(temp1.str+len, st.str);   // replaces for loop
  36. and of course you wouldn't need the erroneous statement, either, then.
  37. --
  38. Russell Blackadar,   russell@mdli.com
  39.